<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 6/3/21
 * Time: 12:21 AM
 */

namespace app\api\service\product;


use app\api\model\CouponUser;
use app\api\model\ProductOrder;
use app\api\model\ProductOrderContent;
use app\api\model\User;
use app\api\service\Token;
use app\lib\exception\ParameterException;

class DeleteOrderSer extends Token
{
    protected  $user;

    protected $productOrder;



    function __construct($id)
    {
        $this->user = User::get(self::getCurrentUid());
        $this->productOrder = ProductOrder::get($id);
    }

    // 入口方法
    public function entrance()
    {
        if($this->productOrder['status'] > 1) {
            throw new ParameterException([
                'msg' => [
                    'order' => '订单状态错误'
                ]
            ]);
        } else {
            ProductOrder::destroy($this->productOrder['id']);
            ProductOrderContent::destroy(['product_order_id'=>$this->productOrder['id']]);
            if($this->productOrder['coupon_id'] > 0) {
                CouponUser::where('id',$this->productOrder['coupon_id'])->setInc('surplus_times');
            }
            return writeJson(203, [], '订单删除成功！');
        }
    }

}